package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

// 专门用来操作session
public abstract class UserContextUtil {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";

    // 返回HttpSession
    // ServletRequestAttributes 是对请求对象 响应对象 HttpSession 的封装
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    // 往session中存入员工
    public static void setEmployee(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }
    // 往session中取出员工
    public static Employee getEmployee(){
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }
    // 从session中移除员工
    public static void removeEmployee(){
        getSession().removeAttribute(EMPLOYEE_IN_SESSION);
    }
    // 往session存入权限表达式
    public static void setExpressions(List<String> expressions){
        getSession().setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }
    // 往session取出权限表达式
    public static List<String> getExpressions(){
        return (List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION);
    }
}
